package cn.itcast.nems.base.service.impl;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.base.api.OriginChannelApi;
import cn.itcast.nems.base.dto.OriginChannelDTO;
import cn.itcast.nems.base.service.OriginChannelService;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.enumeration.BaseErrorBodyEnum;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.manager.icc.DictionaryV2Manager;
import cn.itcast.nems.manager.icc.model.DictionaryEditModel;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class OriginChannelServiceImpl implements OriginChannelService, OriginChannelApi {
    @Value("${icc.dictionary.originChannelId}")
    private Integer originChannelId;
    
    @Autowired
    private DictionaryV2Manager dictionaryV2Manager ;
    
    @Override
    public Page<OriginChannelDTO> findPageList(PageQueryModel<OriginChannelDTO> queryModel)  {
        OriginChannelDTO channelDTO = queryModel.getCondition() ;
        channelDTO.setParentId(originChannelId);
        queryModel.setCondition(channelDTO);
        String result = null;
        try {
            result = dictionaryV2Manager.findPageList(JSON.toJSONString(queryModel));
        } catch (IOException e) {
            BizExceptionProducer.throwProduce(e, BaseErrorBodyEnum.ICC_BAD_REQUEST);
        }catch (RuntimeException e) {
            BizExceptionProducer.throwProduce(e, NemsErrorBodyEnum.PARAM_ERROR ,e.getMessage());
        }
        log.info("[查询传智云订单来源分页数据] 查询结果: {}", result);
        if (StringUtils.hasText(result)) {
            Page<OriginChannelDTO> page = JSON.<Page<OriginChannelDTO>>parseObject(result, Page.class);
            List<OriginChannelDTO> list =  JSON.parseArray(JSON.parseObject(result).getString("records"),OriginChannelDTO.class) ;
            page.setRecords(list);
            return page;
        }
        return null;
    }

    @Override
    public String addOriginChannel(OriginChannelDTO channelDTO)  {
        DictionaryEditModel editModel = new DictionaryEditModel() ;
        BeanUtils.copyProperties(channelDTO , editModel) ;
        editModel.setParentId(originChannelId);
        try {
            return dictionaryV2Manager.add( editModel) ;
        } catch (IOException e) {
            BizExceptionProducer.throwProduce(e,BaseErrorBodyEnum.ICC_BAD_REQUEST);
        }catch (RuntimeException e) {
            BizExceptionProducer.throwProduce(e, NemsErrorBodyEnum.PARAM_ERROR ,e.getMessage());
        }
        return null;
    }

    @Override
    public String updateOriginChannel(OriginChannelDTO channelDTO)  {
        DictionaryEditModel editModel = new DictionaryEditModel() ;
        BeanUtils.copyProperties(channelDTO , editModel) ;
        try {
            return dictionaryV2Manager.update( editModel) ;
        } catch (IOException e) {
            BizExceptionProducer.throwProduce(e,BaseErrorBodyEnum.ICC_BAD_REQUEST);
        }catch (RuntimeException e) {
            BizExceptionProducer.throwProduce(e, NemsErrorBodyEnum.PARAM_ERROR ,e.getMessage());
        }
        return null;
    }

    @Override
    public List<OriginChannelDTO> findByParam(OriginChannelDTO queryModel) {
        Assert.notNull(queryModel , "参数为空");
        queryModel.setParentId(originChannelId);
        String result = null;
        try {
            result = dictionaryV2Manager.findListByParam(JSON.toJSONString(queryModel));
        } catch (IOException e) {
            BizExceptionProducer.throwProduce(e,BaseErrorBodyEnum.ICC_BAD_REQUEST);
        }catch (RuntimeException e) {
            BizExceptionProducer.throwProduce(e, NemsErrorBodyEnum.PARAM_ERROR ,e.getMessage());
        }
        return JSON.parseArray(result , OriginChannelDTO.class);
    }

    @Override
    public String updateState(OriginChannelDTO channelDTO)  {
        DictionaryEditModel editModel = new DictionaryEditModel() ;
        BeanUtils.copyProperties(channelDTO , editModel) ;
        try {
            return dictionaryV2Manager.updateState(editModel ) ;
        } catch (IOException e) {
            BizExceptionProducer.throwProduce(e,BaseErrorBodyEnum.ICC_BAD_REQUEST);
        }catch (RuntimeException e) {
            BizExceptionProducer.throwProduce(e, NemsErrorBodyEnum.PARAM_ERROR ,e.getMessage());
        }
        return null;
    }

    @Override
    public OriginChannelDTO findById(Integer id) {
        String result = null;
        try {
            result = dictionaryV2Manager.findId( id);
            return JSON.parseObject(result, OriginChannelDTO.class);
        } catch (Exception e) {
            BizExceptionProducer.throwProduce(e,BaseErrorBodyEnum.ICC_BAD_REQUEST);
        }
        return null;
    }
}
